Skip to main content

alerts.js

/common/alerts.js is a common file to both backend repos to POST new alerts and PATCH existing ones. It also contains a cache JSON object used to delay the posting of new alerts for a declared number of run cycles.

Features

  • POST new alerts
  • PATCH existing alerts
  • Determines if an alert can be closed
  • Delay new alerts

Cache delay

Delay the POSTING of certain alerts by way of the delayJSON object. The alerts to delay are held in a configuration file stored in the database. Use teh Centurion UI to updated and refine those alerts to delay.

{
"notes": "Changes require a restart of centurionV2-workers.",
"templates": {
"createPricesNoDataAlertObj": 3,
"createWalletsNoDataAlertObj": 3
}
}

The configuration file is used to populate the internal JSON object that becomes the delayCache object.

let delayJSON = {
templates: { createPricesNoDataAlertObj: 3, createWalletsNoDataAlertObj: 3 },
aliases: {},
};

How it works:

  1. When a new alert is inbound, and the alert's template is in the delayJSON object, the alert will be delayed.

  2. If the alert's template exists but the alert's alias does not, then the alias is added to the key aliases along with a counter value of 1. The cache waits for the worker's next run cycle.

    {
    templates: { "createPricesNoDataAlertObj": 3,
    "createWalletsNoDataAlertObj": 3 },
    aliases: {"walletsWorker-348348877", 1},
    }
  3. On the worker's next run cycle, if the alias counter does not equal the template counter, then the alias counter is incremented and the alert is not POSTED. The cache waits for the worker's next run cycle.

  4. On the worker's next run cycle, if the alias counter equals the template counter, then the alert is posted.

Clear the alias

It is up to the worker to clear its own aliases when a condition no longer exists.

const { createWalletsNoDataAlertObj, getBaseAlias } = require("./alertTemplates");
...
if (
!chain.testnet && alert.data.length === Object.keys(chain.providers.rpc).length
){
sendAlert(await createWalletsNoDataAlertObj(alert));
}
else {
removeAlias(getBaseAlias(chain.network_id));
}